package com.zhangtai.modules.service.impl.security;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangtai.modules.controller.aouthLogin.LoginController;
import com.zhangtai.modules.dao.SecurityDao;
import com.zhangtai.modules.dto.SecurityDto;
import com.zhangtai.modules.entity.security.SecurityEntity;
import com.zhangtai.modules.service.security.SecurityService;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;


@RestController
public class SecurityServiceImpl extends ServiceImpl<SecurityDao, SecurityEntity> implements SecurityService {

    @Autowired
    private SecurityDao securityDao;


    @Override
    public PageResult queryPage(@RequestParam(value = "keyword", required = false) String keyword,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        IPage<SecurityEntity> page = this.page(
                new Page<SecurityEntity>(pageNum,pageSize),
                new QueryWrapper<SecurityEntity>().like(!StringUtils.isEmpty(keyword),"name",keyword)
                        .eq("is_delete",0)
        );

        return new PageResult(page);
    }

    @Override
    public PageResult getPersonSecurity(@RequestParam(value = "oid", required = false) Long oid,
                                        @RequestParam(value = "type", required = false) Integer type,
                                        @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        IPage<SecurityEntity> page = this.page(
                new Page<>(pageNum,pageSize),
                new QueryWrapper<SecurityEntity>().eq("type",type).eq("is_delete",0)
                        .eq("oid",oid)
                .orderByDesc("create_time")
        );
        return new PageResult(page);

    }

    /**
     * 获取社保公积金列表
     * @param pageNum
     * @param pageSize
     * @param securityName
     * @param unitName
     * @param status
     * @param number
     * @param type
     * @return
     */
    @Override
    public PageResult<SecurityEntity> getSecurityList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                      @RequestParam(value = "securityName", required = false) String securityName,
                                                      @RequestParam(value = "unitName", required = false) String unitName,
                                                      @RequestParam(value = "cityName", required = false) String cityName,
                                                      @RequestParam(value = "status", required = false) Integer status,
                                                      @RequestParam(value = "number", required = false) Long number,
                                                      @RequestParam(value = "type", required = false) Integer type){
        IPage<SecurityEntity> page = this.page(
                new Page<SecurityEntity>(pageNum,pageSize),
                new QueryWrapper<SecurityEntity>().like(!StringUtils.isEmpty(securityName),"security_name",securityName)
                        .eq(!StringUtils.isEmpty(unitName)&&unitName!="","unit_name",unitName)
                        .eq(!StringUtils.isEmpty(cityName)&&cityName!="","city_name",cityName)
                        .eq(!ObjectUtils.isEmpty(status),"status",status)
                        .eq(!ObjectUtils.isEmpty(number),"number",number)
                        .eq(!ObjectUtils.isEmpty(type),"type",type)
                        .eq("is_delete",0)
        );

        return new PageResult(page);
    }





    /**
     * 获取社保公积金列表
     * @param pageNum
     * @param pageSize
     * @param securityName
     * @param unitName
     * @param status
     * @param number
     * @param type
     * @return
     */
    @Override
    public PageResult<SecurityDto> getSecurityList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                   @RequestParam(value = "contractIds") List<Long> contractIds,
                                                   @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                   @RequestParam(value = "securityName", required = false) String securityName,
                                                   @RequestParam(value = "unitName", required = false) String unitName,
                                                   @RequestParam(value = "cityName", required = false) String cityName,
                                                   @RequestParam(value = "status", required = false) Integer status,
                                                   @RequestParam(value = "number", required = false) Long number,
                                                   @RequestParam(value = "type", required = false) Integer type,
                                                   @RequestParam(value = "startTime", required = false) String startTime,
                                                   @RequestParam(value = "endTime", required = false) String endTime){
        Page<SecurityDto> page =  new Page<SecurityDto>(pageNum,pageSize);
        //获取登录用户id
//        loginController.getUserInfo();


        List<SecurityDto> list = securityDao.getSecurityList(contractIds,startTime,endTime,securityName,unitName,cityName,status,number,type,page);
        page.setRecords(list);
        return new PageResult(page);
    }

    @Override
    public List<SecurityEntity> getAllSecurityList(@RequestParam(value = "type", required = false) Integer type){
        List<SecurityEntity> list = this.list(new QueryWrapper<SecurityEntity>().eq("type",type).eq("status","0").eq("is_delete", 0));
        return list;
    }



    @Override
    public SecurityEntity getByIdE(Long id) {
            SecurityEntity one = this.getOne(new QueryWrapper<SecurityEntity>().eq("security_id", id).eq("is_delete", 0));

        return one;
    }


    @Override
    public SecurityEntity getByUnitName(String unitName,Integer type){
        SecurityEntity one = this.getOne(new QueryWrapper<SecurityEntity>().eq("unit_name", unitName).eq("is_delete", 0).eq("type",type));

        return one;
    }

    @Override
    public R saveE(SecurityEntity entity) {
        boolean b = this.save(entity);
        if (!b){
            return R.error();
        }
        return R.ok(entity);

    }

    @Override
    public R updateByIdE(SecurityEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R removeOneById(Long id) {
            SecurityEntity entity = new SecurityEntity();
        entity.setSecurityId(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()){
                return r;
            }
        }
        return R.ok();
    }

}